﻿
Imports std
Imports elpmis
Imports webctrl

'not fully necessary, just show the usage of scriptparser & make each call can be called directly
Public Class lpmsapp
    Inherits scriptapp

    Private funcs As [set](Of String) = Nothing

    Protected Sub New(ByVal ifuncs As vector(Of [function]))
        MyBase.New(ifuncs)
        funcs = New [set](Of String)()
        If Not ifuncs Is Nothing Then
            Dim i As Int64
            For i = 0 To ifuncs.size() - 1
                funcs.insert(ifuncs(i).name())
            Next
        End If
    End Sub

    Public Shared Shadows Function parse(ByVal script As String, ByRef output As lpmsapp) As Boolean
        Dim funcs As vector(Of [function]) = Nothing
        If scriptapp.parseImpl(script, funcs) Then
            output = New lpmsapp(funcs)
            Return True
        Else
            Return False
        End If
    End Function

    Private Shadows Function invoke(ByVal funcname As String, _
                                    Optional ByVal parameters As parameters_t = Nothing, _
                                    Optional ByRef rtn As bare_variable = Nothing) As Boolean
        'all the function called in lpmsapp should be script function, instead of internalFunctions directly
        'otherwise, it should call mybase.invoke, and may got some error message
        Return funcs.find(funcname) <> funcs.end() AndAlso MyBase.invoke(funcname, parameters, rtn)
    End Function

    Public Function process(Optional ByRef rtn As bare_variable = Nothing) As Boolean
        Return invoke("process", rtn:=rtn)
    End Function

    Public Function header(Optional ByRef rtn As bare_variable = Nothing) As Boolean
        Return invoke("header", rtn:=rtn)
    End Function

    Public Function initial(ByVal config As configParser, Optional ByRef rtn As bare_variable = Nothing) As Boolean
        Dim parameters As parameters_t = Nothing
        parameters = New parameters_t()
        parameters.push_back(make_pair("config", New bare_variable(typeDefination.var, config)))
        parameters.push_back(make_pair("inlineScript", New bare_variable(typeDefination.var, New inlineScript())))
        Return invoke("initial", parameters, rtn)
    End Function

    'path & cache are optional, do not want to write too much useless log
    Public Function path(Optional ByRef rtn As bare_variable = Nothing) As Boolean
        If Not invoke("path", rtn:=rtn) Then
            rtn = Nothing
        End If
        Return True
    End Function

    Public Function cache(Optional ByRef rtn As bare_variable = Nothing) As Boolean
        If Not invoke("cache", rtn:=rtn) Then
            rtn = Nothing
        End If
        Return True
    End Function

    Public Function render(Optional ByVal parameters As iParameters = Nothing, _
                           Optional ByRef rtn As bare_variable = Nothing) As Boolean
        Return invoke("render", parameters, rtn)
    End Function

    'not a good way here, but it's really convenient
    Public Function global_initial(Optional ByRef rtn As bare_variable = Nothing) As Boolean
        If Not isemptystring(globalInitial) Then
            Return invokeShared(globalInitial, rtn:=rtn)
        Else
            Return True
        End If
    End Function
End Class
